package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.constant.OperatorConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.Strategy;
import com.qf.service.api.CacheService;
import com.qf.util.CheckMobileOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 号段补全策略/流量控制
 * @author 何星宇
 * @version V1.0
 * @Date 2021/11/23 19:32
 */

@Service(value = "phase")
@Slf4j
public class MobilePhaseStrategy implements Strategy {

    @Autowired
    private CacheService cacheService;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        //记录日志
        log.info("【策略模块-号段补全】 策略开始执行！ standardSubmit={}",standardSubmit);

        //1.获取短信发送的目标手机号
        String destMobile = standardSubmit.getDestMobile();

        //2.根据手机号判断手机号的运营商
        if (CheckMobileOperator.isChinaMobilePhoneNum(destMobile)){
            //中国移动
            standardSubmit.setOperatorId(OperatorConstants.CHINA_MOBILE);
            //设置网关
            standardSubmit.setGatewayID(1);
        }else if (CheckMobileOperator.isChinaUnicomPhoneNum(destMobile)){
            //中国联通
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNICOM);
            //设置网关
            standardSubmit.setGatewayID(2);
        }else if (CheckMobileOperator.isChinaTelecomPhoneNum(destMobile)){
            //中国电信
            standardSubmit.setOperatorId(OperatorConstants.CHINA_TELECOM);
            //设置网关
            standardSubmit.setGatewayID(3);
        }else {
            //未知号码
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNKNOWN);
            //设置网关
            standardSubmit.setGatewayID(0);
        }

        //3.获取目标手机号的前七位
        String subMobile = destMobile.substring(0, 7);

        //4.根据手机号前七位判断手机号的归属地，省、市
        String provinceAndCity = cacheService.get(CacheConstants.CACHE_PREFIX_PHASE + subMobile);
        if (provinceAndCity==null){
            log.info("【策略模块-号段补全】  目标号码归属地为空  destMobile={}",destMobile);
            return false;
        }

        String[] provinceAndCityArray = provinceAndCity.split(",");
        if (provinceAndCityArray.length<2){
            log.info("【策略模块-号段补全】  目标号码归属地为空  destMobile={}",destMobile);
            return false;
        }
        standardSubmit.setProvinceId(Integer.valueOf(provinceAndCityArray[0]));
        standardSubmit.setCityId(Integer.valueOf(provinceAndCityArray[1]));

        //5.获取通道数据
        Map<String,String> channel = cacheService.hMGet(CacheConstants.CACHE_PREFIX_SMS_CHANNEL +
                standardSubmit.getOperatorId() +
                ":" +
                standardSubmit.getGatewayID()
        );
        if (channel==null||channel.size()==0){
            log.info("【策略模块-号段补全】  通道数据为空  standardSubmit={}",standardSubmit);
            return false;
        }

        //6.获取路由数据
        Map<String,String> router = cacheService.hMGet(CacheConstants.CACHE_PREFIX_ROUTER +
                standardSubmit.getClientID() +
                ":" +
                channel.get("id")
        );
        if (router==null||router.size()==0){
            log.info("【策略模块-号段补全】  路由数据为空  standardSubmit={}",standardSubmit);
            return false;
        }

        //7.补全短信对象中的下发源号码
        String spNumber = channel.get("spnumber");
        String extendNumber = router.get("extendnumber");
        standardSubmit.setSrcNumber(spNumber+extendNumber);

        //8.根据运营商不同，补全这条短信需要的费用
        standardSubmit.setFee(Long.parseLong(router.get("price")));


        //记录日志
        log.info("【策略模块-号段补全】 策略执行完毕！ standardSubmit={}",standardSubmit);

        return true;
    }
}
